package com.flower.background.controller;

import com.flower.entity.Admin;
import com.flower.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

import java.util.ArrayList;
import java.util.List;

/**
 * 自定义认证服务
 */
public class UserDetailsServiceImpl implements UserDetailsService {

    @Autowired
    private AdminService sellerService;
    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        //根据用户名，查询商家信息

        Admin seller = sellerService.findByName(username);
        if (seller!=null){
//      创建List集合封装角色
            List <GrantedAuthority> authorities=new ArrayList();
//            添加角色
            authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

            User user=new User(username,seller.getPassword(),authorities);
            return user;

        }
        return null;
    }

}
